package com.yimi.yinhepda.db.helper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.pda.uce.commom.util.LogUtil;
import com.yimi.yinhepda.base.BaseApplication;

import java.io.File;

/**
 * 需要实现该抽象类类进行通过实体进行创建表
 *
 * @author 000432
 */
public class DbHelperTemp extends OrmLiteSqliteOpenHelper {

    public static final String TAG = DbHelperTemp.class.getName();
    /**
     * 数据库版本号
     */
    private static final int TABLE_VERSION = DbHelper.TABLE_VERSION;
    /**
     * 数据库文件名称
     */
    public static final String TABLE_NAME = "ym-pda-temp.db";
    /**
     * 数据库文件夹
     */
    public static final String DB_FILE_DIR = "database/temp";
    /**
     * 数据库路径
     */
    public static String DB_PATH;

    private volatile static DbHelperTemp INSTANCE;

    static {
        File mFile = BaseApplication.getContext().getExternalFilesDir(DB_FILE_DIR);
        if (mFile != null) {
            DB_PATH = mFile.getAbsolutePath() + File.separator;
            if (!mFile.exists()) {
                if (mFile.mkdirs()) {
                    LogUtil.e("初始化database到内存卡成功");
                } else {
                    DB_PATH = "";
                    LogUtil.e("初始化database到机身内存成功");
                }
            }
        }
    }

    public DbHelperTemp(Context context) {
        super(context, DB_PATH + TABLE_NAME, null, TABLE_VERSION);
    }

    /**
     * 数据库Helper 获取实例化
     */
    public static DbHelperTemp getInstance(Context context) {
        if (INSTANCE == null) {
            synchronized (DbHelperTemp.class) {
                if (INSTANCE == null) {
                    INSTANCE = new DbHelperTemp(context);
                }
            }
        }
        return INSTANCE;
    }

    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {

    }

    @Override
    public void close() {
        super.close();
        INSTANCE = null;
    }
}
